/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.cef.generator.entityGenerator.entity.entityData;


import com.inspur.edp.caf.generator.BaseGenerator;
import com.inspur.edp.caf.generator.baseInfo.AnnotationInfo;
import com.inspur.edp.caf.generator.baseInfo.AnnotationType;
import com.inspur.edp.caf.generator.baseInfo.TypeInfo;
import com.inspur.edp.cef.api.attr.RootAttribute;
import com.inspur.edp.cef.designtime.api.IGspCommonField;
import com.inspur.edp.cef.entity.entity.IChildEntityData;
import com.inspur.edp.cef.entity.entity.IEntityData;
import com.inspur.edp.cef.gencommon.MediateType;
import com.inspur.edp.cef.generator.entityGenerator.BaseEntityGeneratorContext;
import com.inspur.edp.cef.generator.entityGenerator.base.dataTypeData.DataTypeDataGenerator;
import com.inspur.edp.cef.generator.entityGenerator.base.dataTypeData.DataTypeDataGeneratorContext;
import com.inspur.edp.cef.generator.entityGenerator.base.dataTypeData.dataTypePropGen.CmNormalDataPropertyGenerator;
import com.inspur.edp.cef.generator.entityGenerator.base.dataTypeData.dataTypePropGen.NormalDataPropertyGenerator;
import com.inspur.edp.cef.generator.entityGenerator.base.nodeConst.DataTypeConstGenerator;
import com.inspur.edp.cef.generator.entityGenerator.base.nodeConst.DataTypeConstTableNameGenerator;
import com.inspur.edp.cef.generator.entityGenerator.entity.EntityNodeGeneratorContext;
import com.inspur.edp.das.commonmodel.IGspCommonObject;
import org.eclipse.jdt.core.dom.QualifiedName;
import org.eclipse.jdt.core.dom.SimpleName;
import org.eclipse.jdt.core.dom.StringLiteral;

import java.util.ArrayList;
import java.util.Arrays;

public class EntityDataGenerator extends DataTypeDataGenerator {
  protected EntityDataGenerator(
          EntityNodeGeneratorContext nodeGenContext, IGspCommonObject commonObject) {
    super(nodeGenContext, commonObject);
  }

  @Override
  protected ArrayList<TypeInfo> addExtendParentInterfaces() {
    ArrayList<TypeInfo> result = super.addExtendParentInterfaces();
    if(result  == null){
      result = new ArrayList<>();
    }
    if (getCommonObject().getParentObject() == null) {
      //TODO: caf暂不支持, 主动Import
      super.nodeGenContext.getEntityGenContext().addImport(IEntityData.class.getName());
      result.add(new TypeInfo(IEntityData.class));
    } else {
      //TODO: caf暂不支持, 主动Import
      super.nodeGenContext.getEntityGenContext().addImport(IChildEntityData.class.getName());
      result.add(new TypeInfo(IChildEntityData.class));
    }
    return result;
  }

  private IGspCommonObject getCommonObject() {
    return (IGspCommonObject) super.getDataType();
  }

  @Override
  protected ArrayList<BaseGenerator> createDataTypeExtendGenerators() {
    ArrayList<BaseGenerator> list = new ArrayList<BaseGenerator>();

    for (IGspCommonObject item : getCommonObject().getContainChildObjects()) {
      if (item.getIsRef()) {
        list.add(
            getExtendDetailPropGen(
                item,
                (EntityNodeGeneratorContext)
                    ((getContext().getDataTypeGeneratorContext() instanceof EntityNodeGeneratorContext)
                        ? getContext().getDataTypeGeneratorContext()
                        : null)));
      } else {

        list.add(
            getDetailPropGen(
                item,
                (EntityNodeGeneratorContext)
                    ((getContext().getDataTypeGeneratorContext() instanceof EntityNodeGeneratorContext)
                        ? getContext().getDataTypeGeneratorContext()
                        : null), item.getCode()));
        if(!item.getCode().equals(toUpperCaseFirstOne(item.getCode()))){//子对象首字母是小写
          list.add(
                  getDetailPropGen(
                          item,
                          (EntityNodeGeneratorContext)
                                  ((getContext().getDataTypeGeneratorContext() instanceof EntityNodeGeneratorContext)
                                          ? getContext().getDataTypeGeneratorContext()
                                          : null), toUpperCaseFirstOne(item.getCode())));
        }
      }
    }
    return list;
  }
  public String toUpperCaseFirstOne(String s){
    if(Character.isUpperCase(s.charAt(0)))
      return s;
    else
      return (new StringBuilder()).append(Character.toUpperCase(s.charAt(0))).append(s.substring(1)).toString();
  }
  protected ExtendDetailListPropGenerator getExtendDetailPropGen(
      IGspCommonObject node, EntityNodeGeneratorContext nodeContext) {
    return new ExtendDetailListPropGenerator(node, nodeContext);
  }

  protected DetailListPropGenerator getDetailPropGen(
          IGspCommonObject node, EntityNodeGeneratorContext nodeContext) {
    return new DetailListPropGenerator(node, nodeContext);
  }

  protected DetailListPropGenerator getDetailPropGen(
          IGspCommonObject node, EntityNodeGeneratorContext nodeContext, String nodeCode) {
    return new DetailListPropGenerator(node, nodeContext, nodeCode);
  }

  @Override
  protected void dataTypeDataAfterGenerate() {
    super.dataTypeDataAfterGenerate();
    BaseEntityGeneratorContext entityContext =getContext().getDataTypeGeneratorContext().getEntityGenContext();
    if (getCommonObject().getParentObject() == null) {
      entityContext.setRootNodeType(
          new MediateType(nodeGenContext.getEntityGenContext().getDefaultPackageName(), getName()));
      entityContext.setRootNodeCode(getCommonObject().getCode());
    }
  }

  @Override
  protected NormalDataPropertyGenerator getNormalDataPropertyGenerator(IGspCommonField field,
                                                                       DataTypeDataGeneratorContext Context) {
    return new CmNormalDataPropertyGenerator(field, Context);
  }

  @Override

  protected NormalDataPropertyGenerator getNormalDataPropertyGenerator(IGspCommonField field,
                                                                       DataTypeDataGeneratorContext Context,  boolean toUpper) {
    return new CmNormalDataPropertyGenerator(field, Context, toUpper);
  }
}
